#include <func.h>

// epoll实现取消重现（实际是增加新的连接）
int main(int argc, char *argv[])
{
    ARGS_CHECK(argc, 3);
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(sfd, -1, "socket");
    struct sockaddr_in addr;
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    // 允许地址重用否则会有一个time_wait 60s默认
    int optVal = 1;
    int ret = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(optVal));
    ERROR_CHECK(ret, -1, "setsockopt"); //


    ret = bind(sfd, (struct sockaddr *)&addr, sizeof(addr));
    ERROR_CHECK(ret, -1, "bind");

    ret = listen(sfd, 10);
    ERROR_CHECK(ret, -1, "listen");

   // int netfd = accept(sfd, NULL, NULL);
   // ERROR_CHECK(netfd, -1, "netfd");
   
 // 创建一个epoll
    int epfd = epoll_create(1);
    ERROR_CHECK(epfd, -1, "epoll");
    // 创建一个epoll结构体和一个就绪的epoll结构体数组// 把stdin以读时间加入监听
    struct epoll_event event,readyArr[2];
    event.data.fd = STDIN_FILENO;
    event.events = EPOLLIN;
    epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &event); 
    // 把已经连接的socket以读的事件加入监听；
    event.data.fd = sfd;
    event.events = EPOLLIN;
    epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event); 

     char buf[4096] = {0};
   
     int netfd = 0;
    int isConnected = 0;
    time_t lastMsg, check;
   
    while (1)
    {
        int readyNum = epoll_wait(epfd, readyArr, 2, -1); // epoll_wait的返回值是就绪事件的个数；
        puts("epoll_wair returns");
        printf("readyNum = %d\n",readyNum);
        if (readyNum == 0 && isConnected != 0)
        {
            puts("time out");
            check = time(NULL);
            if (check - lastMsg > 5)
            {
                send(netfd, "You are free", 12, 0);
                close(netfd);
                event.data.fd = netfd;
                event.events = EPOLLIN;
                epoll_ctl(epfd, EPOLL_CTL_DEL, netfd, &event);
                isConnected = 0;
            }
        }
        for (int i = 0; i < readyNum; i++)
        {
            if (readyArr[i].data.fd == STDIN_FILENO)
            {
                if (isConnected == 0)
                {
                    int ret = read(STDIN_FILENO, buf, sizeof(buf));
                    if (ret == 0)
                    {
                        goto end;
                    }
                }
                bzero(buf, sizeof(buf));
                int ret = read(STDIN_FILENO, buf, sizeof(buf));
                if (ret == 0)
                {
                    goto end;
                }
                send(netfd, buf, strlen(buf), 0);
            }
            else if (readyArr[i].data.fd == sfd)
            {
                if (isConnected != 0)
                {
                   int  notfd = accept(sfd, NULL, NULL);
                     close(notfd);
                    continue;
                }
                netfd = accept(sfd, NULL, NULL);
                ERROR_CHECK(netfd, -1, "accept");
                event.data.fd = netfd;
                event.events = EPOLLIN;
                epoll_ctl(epfd, EPOLL_CTL_ADD, netfd, &event);
                isConnected = 1;
                lastMsg = time(NULL);
            }
            else if (readyArr[i].data.fd == netfd)
            {
                bzero(buf, sizeof(buf));
                int ret = recv(netfd, buf, sizeof(buf), 0);
                if (ret == 0)
                {
                    close(netfd);
                    event.data.fd = netfd;
                    event.events = EPOLLIN;
                    epoll_ctl(epfd, EPOLL_CTL_DEL, netfd, &event);
                    isConnected = 0;
                }
                lastMsg = time(NULL);
                puts(buf);
            }
        }
    }
end:

    return 0;
}